//
//  ChooseCreditableCardCell.m
//  MRBang
//
//  Created by 铅笔 on 15/12/16.
//  Copyright © 2015年 杭州易网新科技. All rights reserved.
//

#import "ChooseCreditableCardCell.h"
#import "ChooseCreditableCardModel.h"

@interface ChooseCreditableCardCell ()

@property (nonatomic,strong) UIView *view_topBG;
@property (nonatomic,strong) UIView *view_buttomBG;

@property (nonatomic,strong) UIImageView *imageView_icon;
@property (nonatomic,strong) UILabel *label_bankName;//银行名
@property (nonatomic,strong) UILabel *label_detailText;//详细小字

@property (nonatomic,strong) UIButton *button_right;//右侧按钮

@property (nonatomic,strong) UILabel *label_buttomTop;
@property (nonatomic,strong) UILabel *label_buttomButtom;

@end

@implementation ChooseCreditableCardCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initWithSubviews];
    }
    
    return self;
}

#pragma mark --- 创建视图
- (void) initWithSubviews
{
    
    // 头部
    self.view_topBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 147*PROPORTION_WIDTH)];
    [self addSubview:self.view_topBG];
    
    //imageview
    self.imageView_icon = [[UIImageView alloc] initWithFrame:CGRectMake(29*PROPORTION_WIDTH, 31*PROPORTION_WIDTH, 140*PROPORTION_WIDTH, 90*PROPORTION_WIDTH)];
    [self.view_topBG addSubview:self.imageView_icon];
    self.imageView_icon.layer.cornerRadius = 5.0;
    self.imageView_icon.clipsToBounds = YES;
    
    //银行名
    self.label_bankName = [[UILabel alloc] initWithFrame:CGRectMake(self.imageView_icon.frame.size.width+self.imageView_icon.frame.origin.x+10, self.imageView_icon.frame.origin.y+5, kScreenWidth-self.imageView_icon.frame.size.width-self.imageView_icon.frame.origin.x-80, 20)];
    [self.view_topBG addSubview:self.self.label_bankName];
    self.label_bankName.font = [UIFont systemFontOfSize:Font_SIZE_24];
    
    //detail
    self.label_detailText = [[UILabel alloc] initWithFrame:CGRectMake(self.label_bankName.frame.origin.x, self.label_bankName.frame.origin.y+self.label_bankName.frame.size.height, self.label_bankName.frame.size.width, 30)];
    [self.view_topBG addSubview:self.self.label_detailText];
    self.label_detailText.font = [UIFont systemFontOfSize:12];
    self.label_detailText.textColor = RXDColor(147, 147, 147);
    
    //按钮
    self.button_right = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-166*PROPORTION_WIDTH, self.view_topBG.frame.size.height/2-25,140*PROPORTION_WIDTH, 70*PROPORTION_WIDTH)];
    [self.view_topBG addSubview:self.button_right];
    [self.button_right setBackgroundColor:RXDColor(255, 118, 112)];
    [self.button_right setTitle:@"立即申请" forState:UIControlStateNormal];
    [self.button_right.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_22]];
    self.button_right.layer.cornerRadius = 3.0;
    self.button_right.clipsToBounds = YES;
//    self.button_right.tag = 10;
    
    [self.button_right addTarget:self action:@selector(ImmediateNowApplication) forControlEvents:UIControlEventTouchUpInside];
    
    //底部
    [self initWithButtomSubviews];
}

- (void) initWithButtomSubviews
{
    self.view_buttomBG = [[UIView alloc] initWithFrame:CGRectMake(0,self.view_topBG.frame.size.height, kScreenWidth,134*PROPORTION_WIDTH)];
    [self addSubview:self.view_buttomBG];
    
    //横线
    UIView *view_line  = [[UIView alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,1.0)];
    [self.view_buttomBG addSubview:view_line];
    view_line.backgroundColor = RXDColor(243, 245, 246);
    
    for (int i=0; i<2; i++) {
        CGFloat view_lineX = kScreenWidth/3;
        UIView *view_line  = [[UIView alloc] initWithFrame:CGRectMake(view_lineX*(i+1),0,1.0,116*PROPORTION_WIDTH)];
        [self.view_buttomBG addSubview:view_line];
        view_line.backgroundColor = RXDColor(243, 245, 246);
    }
    
    //横线
    UIView *view_lineButtom  = [[UIView alloc] initWithFrame:CGRectMake(0,self.view_buttomBG.height-20*PROPORTION_WIDTH,kScreenWidth,20.0*PROPORTION_WIDTH)];
    [self.view_buttomBG addSubview:view_lineButtom];
    view_lineButtom.backgroundColor = RXDColor(243, 245, 246);
    
    NSArray *array_title = @[@"额度",@"成功率",@"申请周期"];
    
    for (int i=0; i<3; i++) {
        
        CGFloat labelX = kScreenWidth/3;
        
        self.label_buttomButtom = [[UILabel alloc] initWithFrame:CGRectMake(labelX*i,13+30*PROPORTION_WIDTH, kScreenWidth/3, 26*PROPORTION_WIDTH)];
        [self.view_buttomBG addSubview:self.label_buttomButtom];
        self.label_buttomButtom.text = array_title[i];
        self.label_buttomButtom.textAlignment = NSTextAlignmentCenter;
        self.label_buttomButtom.font = [UIFont systemFontOfSize:Font_SIZE_22];
        self.label_buttomButtom.textColor = RXDColor(132, 132, 132);
    }
}

- (void) layoutSubviews
{
    /**
     *  移除 视图 界面上重影的问题
     */
    [self.label_buttomTop removeFromSuperview];
    UILabel *label_firse = (UILabel *)[self.view_buttomBG viewWithTag:30];
    [label_firse removeFromSuperview];
    UILabel *label_second = (UILabel *)[self.view_buttomBG viewWithTag:31];
    [label_second removeFromSuperview];
    
//    self.imageView_icon.image = [UIImage imageNamed:@"b_bcms"];
    [self.imageView_icon sd_setImageWithURL:[NSURL URLWithString:self.model_choseCredita.img] placeholderImage:nil];
    
    self.label_bankName.text = self.model_choseCredita.name;
    
//    self.label_detailText.text = @"匹配淘宝Vip等级，尊享Vip权益";
    self.label_detailText.numberOfLines = 2;
    [self.label_detailText sizeToFit];

    NSString *str_money = [NSString stringWithFormat:@"%d千-%d万元",[self.model_choseCredita.limitmin intValue]/1000,[self.model_choseCredita.limitmax intValue]/10000];
    
    NSMutableArray *array_titleTop = [[NSMutableArray alloc] init];
    
    [array_titleTop addObject:str_money];
    [array_titleTop addObject:[NSString stringWithFormat:@"%@％",self.model_choseCredita.successrate]];
    [array_titleTop addObject:[NSString stringWithFormat:@"%@个工作日",self.model_choseCredita.applycycle]];
    
    for (int i=0; i<3; i++) {
        
        CGFloat labelX = kScreenWidth/3;
        self.label_buttomTop = [[UILabel alloc] initWithFrame:CGRectMake(labelX*i, 10, kScreenWidth/3, 26*PROPORTION_WIDTH)];
        self.label_buttomTop.tag = 30+i;
        [self.view_buttomBG addSubview:self.label_buttomTop];
        self.label_buttomTop.text = array_titleTop[i];
        self.label_buttomTop.textAlignment = NSTextAlignmentCenter;
        self.label_buttomTop.font = [UIFont systemFontOfSize:Font_SIZE_22];
        
        if (i!=2) {
            self.label_buttomTop.textColor = [UIColor redColor];
        }
    }
}

/**
 *  立即申请
 */
- (void) ImmediateNowApplication
{
    if ([self.immediateDelegate respondsToSelector:@selector(ImmediateApplication:)]) {
        [self.immediateDelegate ImmediateApplication:self.model_choseCredita];
    }

}

@end
